/************************************************/
/************************************************/
/**** PART 4: REPLICATE MAIN RESULTS  ****/
/************************************************/
/************************************************/




********************************************************************************
*******Table 1. Summary Statistics
********************************************************************************
	*Panel A
	sum SameRace

	*Panel B
	fsum  Admit ADI Consulttime,f(%9.3f)

	*Panel C
	fsum  Revisit Death,f(%9.3f)

	*Panel D
	sum Male age ibn.severitylevel 



********************************************************************************
*******Table 2. Race Concordance on Physician Decisions
********************************************************************************

	global xlist Male i.patracegroup i.agegroup i.severitylevel i.consultstarthour i.consultstartdow i.consultstartmonth i.BrDx
	global xlist_exante Male i.patracegroup i.agegroup i.severitylevel i.consultstarthour i.consultstartdow i.consultstartmonth 


	foreach y in Admit ADI lnConsulttime {

		*column (1)
		qui xi:reg `y' SameRace i.phyracegroup i.patracegroup, vce(cluster doctorid)
		qui summ `y' if e(sample)
		outreg2 using Table2, excel append label ctitle(`y') keep(SameRace) bdec(4) sdec(4) rdec(3) adec(3) ///
		addtext(Physician Characteristics, Race, Case Characteristics, Race) addstat(Mean, r(mean)) nocons

		*column (2)
		qui xi:reg `y' SameRace i.phyracegroup $xlist, vce(cluster doctorid)
		qui summ `y' if e(sample)
		outreg2 using Table2, excel append label ctitle(`y') keep(SameRace) bdec(4) sdec(4) rdec(3) adec(3) ///
		addtext(Physician Characteristics, Race, Case Characteristics, All) addstat(Mean, r(mean)) nocons

		*column (3)
		qui xi:reg `y' SameRace i.doctorid $xlist, vce(cluster doctorid)
		qui summ `y' if e(sample)
		outreg2 using Table2, excel append label ctitle(`y') keep(SameRace) bdec(4) sdec(4) rdec(3) adec(3) ///
		addtext(Physician Characteristics, FE, Case Characteristics, All) addstat(Mean, r(mean)) nocons

		*column (4)
		qui xi:reg `y' SameRace i.doctorid $xlist_exante, vce(cluster doctorid)
		qui summ `y' if e(sample)
		outreg2 using Table2, excel append label ctitle(`y') keep(SameRace) bdec(4) sdec(4) rdec(3) adec(3) ///
		addtext(Physician Characteristics, FE, Case Characteristics, Ex ante) addstat(Mean, r(mean)) nocons

	}


********************************************************************************
*******Table 3. Race Concordance on Physician Decisions: IV estimates
********************************************************************************

	*Panel A: first stage
	
	qui reg SameRace Proportion_Samerace i.doctorid $xlist, vce(cluster doctorid)
	test Proportion_Samerace
	local fstat = r(F)
	qui summ SameRace if e(sample)

	outreg2 using Table3, excel append label keep(Proportion_Samerace) ///
	bdec(4) sdec(4) rdec(3) adec(3) addstat(F-statistic, `fstat', Mean, r(mean)) nocons


	*Panel B: second stage

	foreach y in Admit ADI lnConsulttime {

		qui xi:ivregress 2sls `y' (SameRace=Proportion_Samerace) i.doctorid $xlist, first vce(cluster doctorid)
		qui summ `y' if e(sample)
		outreg2 using Table3, excel append label ctitle(`y') keep(SameRace) ///
		bdec(4) sdec(4) rdec(3) adec(3) addstat(Mean, r(mean)) nocons

		estat endogenous

	}


********************************************************************************
*******Table 4. Race Concordance on Patient Outcomes
********************************************************************************

	foreach y in Revisit Death{

		preserve

		drop if `y'==.


		***Panel A: OLS

		qui xi:reg `y' SameRace i.doctorid  $xlist, vce(cluster doctorid)
		qui summ `y' 
		outreg2 using Table4, excel append label ctitle(`y') keep(SameRace) ///
		bdec(4) sdec(4) rdec(3) adec(3) addstat(Mean, r(mean)) nocons


		***Panel B: first stage

		qui reg SameRace Proportion_Samerace i.doctorid $xlist, vce(cluster doctorid)
		test Proportion_Samerace
		local fstat = r(F)
		qui summ SameRace 
		outreg2 using Table4, excel append label keep(Proportion_Samerace) ///
		bdec(4) sdec(4) rdec(3) adec(3) addstat(F-statistic, `fstat', Mean, r(mean)) nocons


		***Panel C: second stage

		qui xi:ivregress 2sls `y' (SameRace=Proportion_Samerace) i.doctorid $xlist, first vce(cluster doctorid)
		qui summ `y'
		outreg2 using Table4, excel append label ctitle(`y') keep(SameRace) ///
		bdec(4) sdec(4) rdec(3) adec(3) addstat(Mean, r(mean)) nocons

		estat endogenous

		restore
		
	}


********************************************************************************
***Table 5. Heterogeneous Analysis: Patient Severity 
********************************************************************************

	foreach y in Admit ADI lnConsulttime Revisit Death{

		qui xi:reg `y' SameRace##severecase i.doctorid $xlist, vce(cluster doctorid)

		lincom 1.SameRace+1.SameRace#1.severecase
		local lb = r(estimate)-invttail(r(df), 0.025)*r(se)
		local ub = r(estimate)+invttail(r(df), 0.025)*r(se)
		
		qui summ `y' if e(sample)

		outreg2 using Table5, excel append label  ctitle(`y') ///
		keep(1.SameRace 1.SameRace#1.severecase) bdec(4) sdec(4) rdec(3) adec(3) ///
		addstat(lb, `lb', ub, `ub', Mean, r(mean)) nocons
		 

	}


********************************************************************************
***Table 6. Heterogeneous Analysis: Diagnostic Category
********************************************************************************

	forvalues i=1(1)4{

		preserve

		keep if SpDx==`i' 

		foreach y in Admit ADI lnConsulttime Revisit Death{

			qui xi:reg `y' SameRace i.doctorid $xlist, vce(cluster doctorid)

			qui summ `y' if e(sample)
			
			outreg2 using Table6, excel append label  ctitle(`y'_`i') keep(SameRace) ///
			bdec(4) sdec(4) rdec(3) adec(3) addstat(Mean, r(mean)) nocons

		}

		restore

	}


********************************************************************************
************Table 7. Heterogeneous Analysis: Physician experience
********************************************************************************

	foreach y in Admit ADI lnConsulttime Revisit Death{

		qui xi:reg `y' SameRace##experienced i.doctorid $xlist, vce(cluster doctorid)

		lincom 1.SameRace+1.SameRace#1.experienced
		local lb = r(estimate)-invttail(r(df), 0.025)*r(se)
		local ub = r(estimate)+invttail(r(df), 0.025)*r(se)
		
		qui summ `y' if e(sample)

		outreg2 using Table7, excel append label ctitle(`y') ///
		keep(1.SameRace 1.SameRace#1.experienced 1.experienced) bdec(4) sdec(4) rdec(3) adec(3) ///
		addstat(lb, `lb', ub, `ub', Mean, r(mean)) nocons

	}



********************************************************************************
************Table 8. diagnosis granularity
********************************************************************************

	foreach y in detailedICD{

		qui xi:reg `y' SameRace i.doctorid $xlist, vce(cluster doctorid)
		
		qui summ `y' if e(sample)
		
		outreg2 using Table8, excel append label  ctitle(`y') keep(SameRace) ///
		bdec(4) sdec(4) rdec(3) adec(3) addstat(Mean, r(mean)) nocons

	}


